<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car_info_model extends CI_Model {

 
    function __construct()
    {
         parent::__construct();
    }
	function select_list()
	{
		 $this->db->from('base_carinfo');
		 $this->db->order_by("carname", "asc"); 


		$query = $this->db->get();

		$result =$query->result_array();

		return $result;

	}
	function select_page($data = array(
			'carname'=>''  //系列名称
			,'brandid'=>''  //品牌id
			,'seriesid'=>''  //类型id
			,'start'=>0		//开始记录数
			,'length'=>0))	//长度
	{
		
		if($data['carname'])
		{
			$this->db->like('carname',$data['carname']);
		}
		if($data['brandid'])
		{
			$this->db->where('brandid',$data['brandid']);
		}
		if($data['seriesid'])
		{
			$this->db->where('seriesid',$data['seriesid']);
		}
	    $this->db->from('base_carinfo');

		//查询总记录数
		$count = $this->db->count_all_results();

		$result['count'] = $count;
		/////////////////////////////////////////////////////////////////
		$this->db->select('base_carinfo.*');
		$this->db->select('base_carbrand.brandname');
		$this->db->select('base_carseries.seriesname');
		if($data['carname'])
		{
			$this->db->like('carname',$data['carname']);
		}
		if($data['brandid'])
		{
			$this->db->where('base_carinfo.brandid ',$data['brandid']);
		}
		if($data['seriesid'])
		{
			$this->db->where('base_carinfo.seriesid ',$data['seriesid']);
		}
	    $this->db->from('base_carinfo');
		$this->db->join('base_carbrand', 'base_carinfo.brandid = base_carbrand.brandid', 'left');
	    $this->db->join('base_carseries', 'base_carinfo.seriesid = base_carseries.seriesid', 'left');

		if(intval($data['length'])<0)
		{
			$data['length'] =10;
		}
		if($data['start'])
		{
			$this->db->limit($data['length'],$data['start']);
		}
		else
		{
			$this->db->limit($data['length'],0);
		}
		$this->db->order_by("carname", "asc"); 


		$query = $this->db->get();

		$result['list']=$query->result_array();
 
		return $result;
	}

	function select_car_list($brandid,$seriesid)	 
	{
		$this->db->select('carid, carname');
		if($brandid)
		{
			$this->db->where('brandid',$brandid);
		}
		if($seriesid)
		{
			$this->db->where('seriesid',$seriesid);
		}
		$this->db->from('base_carinfo');
	 
		$this->db->order_by("carname", "asc"); 

		$query = $this->db->get();

		$result['list']=$query->result_array();

		return $result;
	}



	function select_by_id($carid)
	{
		$this->db->where('base_carinfo.carid',$carid);
		
		$this->db->from('base_carinfo');
		$this->db->join('base_carinfo_extend', 'base_carinfo.carid = base_carinfo_extend.carid', 'left');
		$query = $this->db->get();
		$row = $query->row_array();
		return $row;
	}

	function update($carid,$data=array(),$dataext=array())
	{
		$this->db->where('carid', $carid);
		$this->db->update('base_carinfo', $data); 
		
		if($dataext)
		{
			$this->db->where('carid', $carid);
			$this->db->update('base_carinfo_extend', $dataext); 
		}

	}

	function insert($data=array(),$dataext=array())
	{
 
		$result = $this->db->insert('base_carinfo', $data); 
		$dataext['carid'] = $this->db->insert_id();
		$result = $this->db->insert('base_carinfo_extend', $dataext); 
		return $result;
	}

	function delete($carid)
	{
		$this->db->where('carid',$carid);
		$result = $this->db->delete('base_carinfo_extend'); 
		$this->db->where('carid',$carid);
		$result = $this->db->delete('base_carinfo'); 
		return $result;
	}
}